package com.offcn.servlet;

import com.offcn.bean.User;
import com.offcn.service.Impl.UserServiceImpl;
import com.offcn.service.UserService;
import com.offcn.utils.BaseServlet;
import com.offcn.utils.ObjectJsonUtil;
import com.offcn.utils.Page;
import com.offcn.utils.ReturnResult;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "UserServlet", urlPatterns = {"/UserServlet"})

public class UserServlet extends BaseServlet {

    private ReturnResult returnResult = null;

    private UserService userService = new UserServiceImpl();

    //用户修改
    protected void doUpdateUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String age = request.getParameter("age");
        String sex = request.getParameter("sex");
        String password = request.getParameter("password");
        String status = request.getParameter("status");
        String role = request.getParameter("role");
        String uid = request.getParameter("uid");

        try {
            userService.updateUser(name,phone,age,sex,password,status,role,uid);
            returnResult = new ReturnResult(name + "修改成功",
                    1, null);
        } catch (Exception e) {
            returnResult = new ReturnResult(name + "修改失败",
                    -1, null);
        }
        ObjectJsonUtil.objOrJson(returnResult, response);

    }

    //用户删除
    protected void doDelUsers(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uids = request.getParameter("uids");
        //获取当前登录用户
        User userSession = (User) request.getSession().getAttribute("userSession");
        ReturnResult returnResult = userService.deleteUser(uids, userSession);
        ObjectJsonUtil.objOrJson(returnResult, response);

    }

    //用户添加
    protected void doAddUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String age = request.getParameter("age");
        String sex = request.getParameter("sex");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String status = request.getParameter("status");
        String role = request.getParameter("role");

        try {
            userService.AddUser(name, phone, age, sex, username, password, status, role);
            returnResult = new ReturnResult(username + "添加成功",
                    1, null);
        } catch (Exception e) {
            returnResult = new ReturnResult(username + "添加失败",
                    -1, null);
        }
        ObjectJsonUtil.objOrJson(returnResult, response);
    }

    //判断用户手机号是否存在
    protected void doExistPhone(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String phone = request.getParameter("phone");
        User user = userService.doExistPhone(phone);
        if (user != null) {
            returnResult = new ReturnResult("手机号以及存在",
                    -1, null);
        } else {
            returnResult = new ReturnResult("ok",
                    1, null);
        }
        ObjectJsonUtil.objOrJson(returnResult, response);
    }

    //判断用户是否存在
    protected void doExistUsername(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");

        User user = userService.getUserByUsername(username);

        if (user != null) {
            returnResult = new ReturnResult("用户名存在，请重新输入",
                    1, null);
        } else {
            returnResult = new ReturnResult("用户名不存在",
                    -1, null);
        }
        ObjectJsonUtil.objOrJson(returnResult, response);

    }

    //进行分页查询
    protected void getUserListPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pageSize = request.getParameter("pageSize");
        String pageNo = request.getParameter("pageNo");
        String search = request.getParameter("search");
        System.out.println(pageNo);
        System.out.println(pageSize);
        System.out.println(search);
        try {
            Page<User> page = userService.getUserListPage(pageSize, pageNo, search);
            returnResult = new ReturnResult("", 1, page);
        } catch (Exception e) {
            returnResult = new ReturnResult("", -1, null);
            throw new RuntimeException(e);
        }
        ObjectJsonUtil.objOrJson(returnResult, response);

    }

    //获取session中登陆的名字
    protected void getLongName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        User userSession = (User) session.getAttribute("userSession");
        if (userSession == null) {
            returnResult = new ReturnResult("对不起你没有登陆", -1, null);
        } else {
            returnResult = new ReturnResult("", 1, userSession.getName());
        }

        ObjectJsonUtil.objOrJson(returnResult, response);
    }

    //判断 用户是否登陆的
    protected void doLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        //获取前台传递过来的数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String imageCode = request.getParameter("imageCode");

        //先判断验证码是否正确
        //获取图片上的验证码和用户输入的验证码进行比对
        String sessionImgCode = (String) session.getAttribute("code");//图片上的验证码获取

        /*
                    private String name; // 真实姓名
            private String phone; // 手机号
            private int age; // 年龄
            private int sex; // 性别 0：男  1：女
            private String username; // 用户名
            private String password; // 用户密码
            private String picture; // 用户头像路径
            private int status = 1; // 用户状态 1：启用 2：禁用
            private String createtime; // 注册时间
            private int role = 0; // 用户角色  1：管理员 2：普通用户
                 */

        //1: 先判断验证吗输入正确不
        if (imageCode != null & sessionImgCode != null && imageCode.equals(sessionImgCode)) {
            User user = userService.doLogin(username, password);
            if (user != null) {
                //判断用户是否可用
                if (user.getStatus() == 1) {
                    //用户可用
                    //判断用户的角色，角色为管理员才能登后台
                    if (user.getRole() == 1) {
                        //把当前登录成功用户存入session
                        request.getSession().setAttribute("userSession", user);

                        returnResult = new ReturnResult("【" + user.getName() + "】登录成功，即将跳转主页;",
                                1, null);
                    } else {

                        returnResult = new ReturnResult("您不是管理员，不能登录后台管理系统",
                                -1, null);
                    }
                } else {

                    returnResult = new ReturnResult("您的账号已经禁用，请联系管理员",
                            -1, null);
                }

            } else {
                returnResult = new ReturnResult("账户密码输入错误", -1, null);
            }
        } else {
            returnResult = new ReturnResult("验证吗输入错误", -1, null);

        }

        //吧对象转化为 json 并且 返回
        ObjectJsonUtil.objOrJson(returnResult, response);
    }

}
